from multiprocessing import Process, Queue, current_process


def producer(queue):
    for i in range(5 * 10000):
        queue.put(f"Message {i}")
        print(f"Produced: Message {i}")


def consumer(queue):
    while True:
        message = queue.get()
        if message is None:  # 用于结束循环
            break
        print(f"Consumed: {message}, {current_process().name}")


if __name__ == "__main__":
    queue = Queue()
    producer_process = Process(target=producer, args=(queue,))
    consumer_process = Process(target=consumer, args=(queue,))
    consumer_process2 = Process(target=consumer, args=(queue,))

    producer_process.start()
    consumer_process.start()
    consumer_process2.start()

    producer_process.join()
    queue.put(None)  # 发送结束信号
    queue.put(None)  # 发送结束信号
    consumer_process.join()
    consumer_process2.join()
